from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver=webdriver.Chrome()
# driver.get('https://www.baidu.com')
driver.maximize_window()

# print(driver.name) #当前浏览器
# print(driver.current_url) #当前url
# print(driver.page_source) #源代码

# print(driver.current_window_handle) #当前窗口句柄

# driver.find_element(By.LINK_TEXT,'新闻').click()
# print(driver.window_handles) #当前打开浏览器的所有标签句柄

#退出浏览器
# driver.quit()

# # | id       | id标识     |
# kw=driver.find_element(By.ID,'kw')
# print(kw.id)
# # | size     | 宽高       |
# print(kw.size)
#
# #rect     | 宽高和坐标 |
# print(kw.rect)
#
# # tag_name | 标签名     |
# print(kw.tag_name)
#
# # | text     | 文本内容   |
# news=driver.find_element(By.LINK_TEXT,'新闻')
# print(news.text)
#
# # 退出浏览器
# driver.quit()

#webelement常用方法
# clear()清空内容
# get_attribute()获得属性值
# is_displayed()判断元素是否可见
# is_enabled()判断元素是否可用

class TestCase:
    def __init__(self):
        self.driver=webdriver.Chrome()

    def clear(self):
        self.driver.get('https://www.baidu.com')
        time.sleep(2)
        kw=self.driver.find_element(By.ID,'kw')
        kw.send_keys('天气预报')

        time.sleep(2)
        kw.clear()

        time.sleep(2)
        kw.send_keys('布丁')

    def get_attribute(self):
        # 获取标签html属性
        self.driver.get('https://www.baidu.com')
        kw=self.driver.find_element(By.ID,'kw')
        print(kw.get_attribute('outerHTML'))
        kw.get_attribute('id')
        print(kw.get_attribute('class'))
        print(kw.get_attribute('name'))
        print(kw.get_attribute('autocomplete'))

    def is_displayed(self):
        # 判断标签是否在页面中显示，显示就是True， 不显示False
        self.driver.get('https://www.baidu.com')
        ie=self.driver.find_element(By.XPATH,'//*[@id="form"]/input[1]')
        print(ie.is_displayed())
        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.is_displayed())

    def is_enabled(self):
        # is_enabled()判断元素是否可用, 可用返回True， 不可用返回False
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        btn1 = self.driver.find_element(By.CSS_SELECTOR, 'body > form > input[type=button]:nth-child(16)')
        print(btn1.is_enabled())

        btn2 = self.driver.find_element(By.XPATH, '/html/body/form/input[4]')
        print(btn2.is_enabled())

        btn3=self.driver.find_element(By.XPATH,'/html/body/form/div[2]')
        print(btn3.is_enabled())


if __name__ == '__main__':
    case = TestCase()
    # case.clear()
    # case.get_attribute()
    case.is_displayed()
    # case.is_enabled()

    time.sleep(2)
    case.driver.quit()
